//
//  MoreSetViewController.m
//  ExcelToPlist
//
//  Created by 赵梁 on 2017/3/29.
//  Copyright © 2017年 赵梁. All rights reserved.
//

#import "MoreSetViewController.h"

#import "ConvertKeyModel.h"
#import "ConvertKeySettingManager.h"

@interface MoreSetViewController ()<NSTableViewDelegate,NSTableViewDataSource>
{
    NSMutableArray<ConvertKeyModel*> *_list;
}

@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSButton *deleteBtn;

@end

@implementation MoreSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    _list = [[ConvertKeySettingManager loadConfigFromDisc] mutableCopy];
    if ([_list isKindOfClass:[NSArray class]]) {
        _list = [_list mutableCopy];
    }else {
        _list = [[NSMutableArray alloc] initWithCapacity:0];
    }
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    
}

- (IBAction)addNewColumn:(id)sender {
    [self saveTableData];
    
    [_list addObject:[ConvertKeyModel new]];
    [_tableView reloadData];
}

- (IBAction)deleteSelectedColumn:(id)sender {
    
}

- (IBAction)saveAndOut:(id)sender {
    [self saveTableData];
}

- (void)saveTableData
{
    for (NSInteger i=0; i<_list.count; i++) {
        NSTableRowView *rowView = [_tableView rowViewAtRow:i makeIfNecessary:NO];
        
        NSView *view = [rowView viewAtColumn:0];
        NSTextField *tf = (NSTextField*)[view viewWithTag:10];
        if (tf) {
            _list[i].keyOld = tf.stringValue;
        }
        
        NSView *newView = [rowView viewAtColumn:1];
        NSTextField *newtf = (NSTextField*)[newView viewWithTag:10];
        if (newtf) {
            _list[i].keyNew = newtf.stringValue;
        }
    }
    
    [self saveToDisc];
}

- (IBAction)loadDefaultConfig:(id)sender {
    
    _list = [[ConvertKeySettingManager loadDefaultConvertKey] mutableCopy];
    if (!_list) {
        _list = [NSMutableArray arrayWithCapacity:0];
    }
    [_tableView reloadData];
}

- (void)saveToDisc
{
    [ConvertKeySettingManager saveToDisc:_list];
}

#pragma mark - tableViewDatasource

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [_list count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    return nil;
    /*
    NSString* columnIdentifier = [tableColumn identifier];
    //NSLog(@"%@", columnIdentifier);
    NSDictionary *dic = [_list objectAtIndex:row];
    id val = [dic valueForKey:columnIdentifier];
    return [NSString stringWithFormat:@"%@",val];
    */
}

- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row NS_AVAILABLE_MAC(10_7)
{
    NSString* identifier = [tableColumn identifier];
    NSTableCellView *cell = [tableView makeViewWithIdentifier:identifier owner:nil];
    if( [cell isKindOfClass:[NSTableCellView class]] ){
        
        NSTextField *tf = (NSTextField*)[cell viewWithTag:10];
        ConvertKeyModel *model = [_list objectAtIndex:row];
        
        if([@"old" isEqualToString:identifier]) {
            tf.stringValue = model.keyOld?model.keyOld:@"";
        }else if([@"new" isEqualToString:identifier]) {
            tf.stringValue = model.keyNew?model.keyNew:@"";
        }else {
            return nil;
        }
        return cell;
    }
    return nil;
}


@end
